﻿using BaoGiaVeThang.ServiceFlight;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BaoGiaVeThang
{
    public class BLCrawler : AirLineCrawler
    {
        public BLCrawler(int startDay)
            : base(startDay)
        {

        }

        public override async void UpdateCheapTicketPrice()
        {
            if (domesticRoute.IsBL)
            {
                DateTime departTime = getDate();
                DomesticRoute tempDomesticRoute = domesticRoute;
                try
                {
                    CrawlerClient client = new CrawlerClient();;
                    RootMinPrice root = await client.GetPriceInFareJetstarAsync(1, tempDomesticRoute.ArrivalAirport, 0, departTime, tempDomesticRoute.DepartureAirport,
                        0, false, departTime.AddDays(10));
                        UpdateToDatabase(root, false, false, true, tempDomesticRoute);
                }
                catch
                {
                    countError++;
                   File.AppendAllText("ErrorBL.txt","Depart="+domesticRoute.DepartureAirport+";Arrival="+domesticRoute.ArrivalAirport
                       +";Date="+departTime+"\n");
                }
            }
            Next();
        }
        public override bool isDone()
        {
            int k = MonthInfo.startMonth + 6;
            if (k > 12)
                k = k % 12;
            return monthInfo.month == k;
        }
        public override DateTime getDate()
        {
            int year = DateTime.Now.Year;
            if (DateTime.Now.Month > monthInfo.month)
            {
                year++;
            }
            return new DateTime(year, monthInfo.month, monthInfo.startDay).AddDays(9);
        }
    }
}
